import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等頁面，或者如一些编辑頁面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如組件頁面
 *                                  // 只有一个时，会将那个子路由当做根路由顯示在侧边栏--如引導頁面
 *                                  // 若你想不管路由下面的 children 声明的个數都顯示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直顯示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑導航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递參數
 * roles: ['admin', 'common']       // 访问路由的角色權限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜單權限
 * meta : {
    noCache: true                   // 如果设置為true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的圖標，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置為false，则不会在breadcrumb面包屑中顯示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/appointment',
    component: () => import('@/views/appointment'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: 'index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/index'),
        name: 'Index',
        meta: { title: '首頁', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index'),
        name: 'Profile',
        meta: { title: '個人中心', icon: 'user' }
      }
    ]
  }
]

// 动態路由，基于用戶權限动態去加载
export const dynamicRoutes = [
  {
    path: '/reservation/index/add',
    component: Layout,
    hidden: true,
    permissions:['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/reservation/add'),
        name: 'ReservationAdd',
        meta: { title: '新增预约', activeMenu: '/reservation/index' }
      }
    ]
  },
  {
    path: '/reservation/index/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/reservation/edit'),
        name: 'ReservationEdit',
        meta: { title: '修改预约', activeMenu: '/reservation/index' }
      }
    ]
  },
  {
    path: '/reservation/index/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/reservation/detail'),
        name: 'ReservationDetail',
        meta: { title: '预约詳情', activeMenu: '/reservation/detail' }
      }
    ]
  },
  {
    path: '/customer/index/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/customer/add'),
        name: 'CustomerAdd',
        meta: { title: '新增客戶', activeMenu: '/customer/index' }
      }
    ]
  },
  {
    path: '/customer/index/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/customer/edit'),
        name: 'CustomerEdit',
        meta: { title: '修改客戶', activeMenu: '/customer/index' }
      }
    ]
  },
  {
    path: '/customer/index/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/customer/detail'),
        name: 'CustomerDetail',
        meta: { title: '客戶詳情', activeMenu: '/customer/detail' }
      }
    ]
  },
  // 銀行新增
  {
    path: '/bank/bank/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/bank/add'),
        name: 'BankAdd',
        meta: { title: '新增銀行賬戶', activeMenu: '/bank/bank' }
      }
    ]
  },
  // 銀行編輯
  {
    path: '/bank/bank/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/bank/edit'),
        name: 'BankEdit',
        meta: { title: '修改銀行賬戶', activeMenu: '/bank/bank' }
      }
    ]
  },
  {
    path: '/bank/bank/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/bank/detail'),
        name: 'BankDetail',
        meta: { title: '銀行賬戶詳情', activeMenu: '/bank/bank' }
      }
    ]
  },
  // 预约规则新增
  {
    path: '/reservation/rule/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/rule/add'),
        name: 'RuleAdd',
        meta: { title: '新增預約規則', activeMenu: '/reservation/rule' }
      }
    ]
  },
  // 预约规则修改
  {
    path: '/reservation/rule/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/rule/edit'),
        name: 'RuleEdit',
        meta: { title: '修改預約規則', activeMenu: '/reservation/rule' }
      }
    ]
  },
  {
    path: '/reservation/rule/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/rule/detail'),
        name: 'RuleDetail',
        meta: { title: '預約規則詳情', activeMenu: '/reservation/detail' }
      }
    ]
  },
  // 報價單新增
  {
    path: '/quote/quote/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/quote/add'),
        name: 'QuoteAdd',
        meta: { title: '新增報價單', activeMenu: '/quote/quote' }
      }
    ]
  },
  // 報價單修改
  {
    path: '/quote/quote/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/quote/edit'),
        name: 'QuoteEdit',
        meta: { title: '修改報價單', activeMenu: '/quote/quote' }
      }
    ]
  },
  {
    path: '/quote/quote/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/quote/detail'),
        name: 'QuoteDetail',
        meta: { title: '報價單詳情', activeMenu: '/quote/quote' }
      }
    ]
  },
  // 供應商新增
  {
    path: '/supplier/supplier/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/supplier/add'),
        name: 'SupplierAdd',
        meta: { title: '新增供應商', activeMenu: '/supplier/supplier' }
      }
    ]
  },
  // 供應商修改
  {
    path: '/supplier/supplier/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/supplier/edit'),
        name: 'SupplierEdit',
        meta: { title: '修改供應商', activeMenu: '/supplier/supplier' }
      }
    ]
  },
  {
    path: '/supplier/supplier/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/supplier/detail'),
        name: 'SupplierDetail',
        meta: { title: '供應商詳情', activeMenu: '/supplier/supplier' }
      }
    ]
  },
  // 費用新增
  {
    path: '/cost/cost/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/cost/add'),
        name: 'CostAdd',
        meta: { title: '新增費用', activeMenu: '/cost/cost' }
      }
    ]
  },
  // 費用修改
  {
    path: '/cost/cost/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/cost/edit'),
        name: 'CostEdit',
        meta: { title: '修改費用', activeMenu: '/cost/cost' }
      }
    ]
  },
  {
    path: '/cost/cost/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/cost/detail'),
        name: 'CostDetail',
        meta: { title: '費用詳情', activeMenu: '/cost/cost' }
      }
    ]
  },
  // 發票新增
  {
    path: '/invoice/invoice/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/invoice/add'),
        name: 'InvoiceAdd',
        meta: { title: '新增發票', activeMenu: '/invoice/invoice' }
      }
    ]
  },
  // 發票修改
  {
    path: '/invoice/invoice/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/invoice/edit'),
        name: 'InvoiceEdit',
        meta: { title: '修改發票', activeMenu: '/invoice/invoice' }
      }
    ]
  },
  {
    path: '/invoice/invoice/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/invoice/detail'),
        name: 'InvoiceDetail',
        meta: { title: '發票詳情', activeMenu: '/invoice/invoice' }
      }
    ]
  },
  // 會計新增
  {
    path: '/entry/index/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/entry/add'),
        name: 'EntryAdd',
        meta: { title: '新增會計', activeMenu: '/entry/index' }
      }
    ]
  },
  // 會計修改
  {
    path: '/entry/index/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/entry/edit'),
        name: 'EntryEdit',
        meta: { title: '修改會計', activeMenu: '/entry/index' }
      }
    ]
  },
  {
    path: '/entry/index/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/entry/detail'),
        name: 'EntryDetail',
        meta: { title: '會計詳情', activeMenu: '/entry/index' }
      }
    ]
  },


  // 新增業務科目與會計科目映射
  {
    path: '/tool/subjectMap/add',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/subjectMap/add'),
        name: 'SubjectMapAdd',
        meta: { title: '新增業務科目與會計科目映射', activeMenu: '/tool/subjectMap' }
      }
    ]
  },
  // 修改業務科目與會計科目映射
  {
    path: '/tool/subjectMap/edit',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/subjectMap/edit'),
        name: 'SubjectMapEdit',
        meta: { title: '修改業務科目與會計科目映射', activeMenu: '/tool/subjectMap' }
      }
    ]
  },
  {
    path: '/tool/subjectMap/detail',
    component: Layout,
    hidden: true,
    permissions: ['default'],
    children: [
      {
        path: '',
        component: () => import('@/views/biz/subjectMap/detail'),
        name: 'SubjectMapDetail',
        meta: { title: '業務科目與會計科目映射詳情', activeMenu: '/tool/subjectMap' }
      }
    ]
  },
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser'),
        name: 'AuthUser',
        meta: { title: '分配用戶', activeMenu: '/system/role' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data'),
        name: 'Data',
        meta: { title: '字典數據', activeMenu: '/system/dict' }
      }
    ]
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    hidden: true,
    permissions: ['monitor:job:list'],
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log'),
        name: 'JobLog',
        meta: { title: '调度日誌', activeMenu: '/monitor/job' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
      }
    ]
  }
]

// 防止连续点击多次路由报错
let routerPush = Router.prototype.push;
let routerReplace = Router.prototype.replace;
// push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}
// replace
Router.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch(err => err)
}

export default new Router({
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
